/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.r3000;

import java.io.*;
import java.util.*;

import nfse.misc.GenericFileFilter;

public class R3000FileFilter implements FileFilter {
    int month = -1;

    int day = -1;

    int year = -1;

    int run = -1;

    int month2 = -1;

    int day2 = -1;

    int year2 = -1;

    int run2 = -1;

    File logDir = null;

    String logID = "";

    public R3000FileFilter(Calendar cal, int run, File logDir, String logID) {
        this.logDir = logDir;
        this.logID = logID;
        month = cal.get(Calendar.MONTH) + 1;
        day = cal.get(Calendar.DAY_OF_MONTH);
        year = cal.get(Calendar.YEAR);
        this.run = run;
        if (run == 24) {
            run2 = 1;
            GregorianCalendar cal2 = new GregorianCalendar();
            cal2.setTimeInMillis(cal.getTimeInMillis());
            cal2.set(Calendar.DAY_OF_MONTH, cal2.get(Calendar.DAY_OF_MONTH) + 1);
            month2 = cal2.get(Calendar.MONTH) + 1;
            day2 = cal2.get(Calendar.DAY_OF_MONTH);
            year2 = cal2.get(Calendar.YEAR);
        } else {
            GregorianCalendar cal2 = new GregorianCalendar();
            cal2.setTimeInMillis(cal.getTimeInMillis());
            month2 = cal.get(Calendar.MONTH) + 1;
            day2 = cal.get(Calendar.DAY_OF_MONTH);
            year2 = cal.get(Calendar.YEAR);
            run2 = run++;
        }
    }

    public boolean accept(File file) {
        try {

            // The dir path
            String path = logDir.getAbsolutePath() + "/";

            String yearStr = ("" + year).substring(2);
            // The file name
            path += "shadow.log." + logID + "." + yearStr;
            if (month < 10)
                path += "0";
            path += month + "";
            if (day < 10)
                path += "0";
            path += day + "";
            // System.out.println(file.getAbsolutePath() + "<-->" + path);
            if (!file.getAbsolutePath().startsWith(path))
                return false;
            // if (run-1 < 10)
            // path += "0";
            // path += (run-1) + "0000";

            // System.out.println("FILE: " + file.getAbsolutePath());
            // System.out.println("\tpath=" + path);

            File[] files = logDir.listFiles(new GenericFileFilter(path));
            if (files.length < run)
                return false;

            Arrays.sort(files);

            String filePath = files[run - 1].getAbsolutePath();
            if (!filePath.equals(file.getAbsolutePath()))
                return false;
            // System.out.println("\tfilePath=" + filePath);
            String nextPath = logDir.getAbsolutePath() + "/";
            nextPath += "shadow.log." + logID + ".";

            // System.out.println("\tnextPath=" + nextPath);

            files = logDir.listFiles(new GenericFileFilter(nextPath));
            boolean foundNext = false;
            for (int i = 0; i < files.length; i++) {
                if (files[i].getAbsolutePath().compareTo(filePath) > 0) {
                    foundNext = true;
                    // System.out.println("Found next: " +
                    // files[i].getAbsolutePath() + "<-->" + filePath);
                    break;
                }
            }
            return foundNext;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return false;

    }
}